package com.hdoit.apollo.web.stock.controllers;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.billing.model.Billing;
import com.hdoit.apollo.logical.billing.service.IBillingService;
import com.hdoit.apollo.logical.common.enums.billing.BillingType;
import com.hdoit.apollo.logical.common.enums.outStock.OutStockModifyMark;
import com.hdoit.apollo.logical.shop.model.Customer;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.shop.service.ICustomerService;
import com.hdoit.apollo.logical.stock.model.OutStock;
import com.hdoit.apollo.logical.stock.model.OutStockCollocation;
import com.hdoit.apollo.logical.stock.model.OutStockShow;
import com.hdoit.apollo.logical.stock.service.IOutStockService;
import com.hdoit.apollo.logical.viewmodel.stock.OutStockSearch;
import com.hdoit.apollo.logical.viewmodel.stock.SendOutGoodsInfo;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.ErrorResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年05月28日 17:23:49
 * @comment
 */
@Controller
@RequestMapping(value = "/stock/outStock/")
public class OutStockController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/stock/outStock/";
    }

    @Autowired
    private IOutStockService outStockService;
    
	@Autowired
	private IBillingService billingService;
	
	@Autowired
	private ICustomerService customerService;

    private final String indexViewName="index";
    private final String modifyIndex="modifyIndex";
    
    private final String outputViewName="output";
    private final String detailedViewName="detailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    
    private final String closeOutStock="closeOutStock";//关闭发货单
    
    private final String submitModifyDetailed="submitModifyDetailed";
    private final String submitModifySave="submitModifySave";
    
    private final String affirmModifyDetailed="affirmModifyDetailed";
    private final String affirmModifySave="affirmModifySave";
    private final String exportDetailed="exportDetailed";
    
    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,OutStockSearch search,boolean ajaxPost,String level){

        pagerModel.setTotalAmount(outStockService.queryCountMP(search));
        List<OutStock> outStocks= outStockService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("outStocks", outStocks);
        model.addAttribute("outStockSearch", search);
        model.addAttribute("level", level);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }
    
    @RequestMapping(value = modifyIndex, method = { RequestMethod.GET,RequestMethod.POST })
    public String modifyIndex(Model model,PagerModel pagerModel,OutStockSearch search,boolean ajaxPost){
    	
    	search.setModifyMark(OutStockModifyMark.modify);
        pagerModel.setTotalAmount(outStockService.queryCountMP(search));
        List<OutStock> outStocks= outStockService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("outStocks", outStocks);
        model.addAttribute("outStockSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(modifyIndex);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String outStockDetailed(Model model,String key){
        OutStock outStock=new OutStock();
        if(!StringUtils.isEmpty(key)){
            outStock=outStockService.queryModelById(key);
        }
        model.addAttribute("outStock", outStock);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,OutStock outStock){
        if(StringUtils.isEmpty(outStock.getOutStockId())){
            outStock.setOutStockId(CommonUtils.getKeyId());
            outStock.setCreateDate(DateUtils.getCurrent());
            outStock.setDeleteMark(YesOrNoInteger.no);
            outStock.setCreateUserId(getLoginModel().getLoginId());
            outStock.setCreateUserName(getLoginModel().getLoginName());
            outStock.setModelStatus(ModelStatus.add);
            Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
            outStock.setShopId(shop.getShopId());
        }else{
            outStock.setModifyDate(DateUtils.getCurrent());
            outStock.setModifyUserId(getLoginModel().getLoginId());
            outStock.setModifyUserName(getLoginModel().getLoginName());
            outStock.setModelStatus(ModelStatus.update);
        }
 
        ActionResult actionResult;
        if(outStockService.applyChange(outStock)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("outStock", outStock);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(outStockService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @RequestMapping(value = closeOutStock, method = { RequestMethod.POST},produces = "text/html;charset=UTF-8")
    public @ResponseBody String closeOutStock(Model model,String key){
        OutStock outStock=outStockService.queryModelById(key);
        outStock.setModelStatus(ModelStatus.update);
		outStock.setBillingType(BillingType.close);
    	if(!outStockService.closeOutStock(outStock)){
    		return new JsonActionResult(ManageContainer.ERROR).toJson();
    	}
		
		return new JsonActionResult(ManageContainer.SUCCESS).toJson();
    }
    
    @RequestMapping(value = submitModifyDetailed, method = { RequestMethod.GET})
    public String submitModifyDetailed(Model model,String key){
    	OutStock outStock=new OutStock();
    	Billing billing=new Billing();
        if(!StringUtils.isEmpty(key)){
            outStock=outStockService.queryModelById(key);
            billing =billingService.getBillingByKey(outStock.getBillingId());
            billing.getOutStock().setOutStockDebt(billing.getOutStock().getOutStockDebt()+outStock.getOutStockNum());
            billing.getOutStock().setReturnCargoNum(outStock.getOutStockNum());
            for (OutStockShow outStockShow : billing.getOutStock().getOutStockShows() ) {
            	for (OutStockShow outStockShowYuan : outStock.getOutStockShows() ) {
            		if(outStockShow.getShowId().equals(outStockShowYuan.getShowIdYuan())) {
            			outStockShow.setOutStockShowDebt(outStockShow.getOutStockShowDebt() + outStockShowYuan.getOutStockShowNum());
            			outStockShow.setReturnCargoNumShow(outStockShowYuan.getOutStockShowNum());
            		}
            	}
            	for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
            		for (OutStockShow outStockShowYuan : outStock.getOutStockShows() ) {
    					for (OutStockCollocation outStockCollocationYuan : outStockShowYuan.getOutStockCollocations()) {
    						if(outStockCollocation.getOutStockCollocationId().equals(outStockCollocationYuan.getOutStockCollocationIdYuan())) {
    							outStockCollocation.setCollocationDebt(outStockCollocation.getCollocationDebt() + outStockCollocationYuan.getCollocationNum());
    							outStockCollocation.setReturnCargoNumCollocation(outStockCollocationYuan.getCollocationNum());
    						}
    					}
    				}
            	}
            	
            }
        }

        model.addAttribute("outStockId", key);
        model.addAttribute("billingJson", JsonUtils.writeValueAsString(billing));
        model.addAttribute("billing", billing);
        return view( submitModifyDetailed);
    }
    
    
    @RequestMapping(value = submitModifySave, method = { RequestMethod.POST})
    public String submitModifySave(Model model,Billing billing,SendOutGoodsInfo sendOutGoodsInfo,String outStockId){
        billing.setModifyDate(DateUtils.getCurrent());
        billing.setModifyUserId(getLoginModel().getLoginId());
        billing.setModifyUserName(getLoginModel().getLoginName());
        billing.setModelStatus(ModelStatus.update);
     
        ActionResult actionResult;
        if(outStockService.submitModifySave(billing,sendOutGoodsInfo,outStockId)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("billing", billing);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(submitModifyDetailed);
    }
    
    @RequestMapping(value = affirmModifyDetailed, method = { RequestMethod.GET})
    public String affirmModifyDetailed(Model model,String key){
    	OutStock outStock=new OutStock();
    	OutStock outStockYuan=new OutStock();
        if(!StringUtils.isEmpty(key)){
            outStock=outStockService.queryModelById(key);
            outStockYuan=outStockService.queryModelById(outStock.getOutStockIdYuan());
        }
        model.addAttribute("outStockYuan", outStockYuan);
        model.addAttribute("outStock", outStock);
        return view(affirmModifyDetailed);
    }

    @RequestMapping(value = affirmModifySave, method = { RequestMethod.POST})
    public String affirmModifySave(Model model,String outStockId,String endType){

    	OutStock outStock=new OutStock();
    	OutStock outStockYuan=new OutStock();
        if(!StringUtils.isEmpty(outStockId)){
            outStock=outStockService.queryModelById(outStockId);
            outStockYuan=outStockService.queryModelById(outStock.getOutStockIdYuan());
        }
        ActionResult actionResult;
        if(outStockService.affirmModifySave(outStock,outStockYuan,endType)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("outStockYuan", outStockYuan);
        model.addAttribute("outStock", outStock);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(affirmModifyDetailed);
    }
    
    //导出详细信息
 	@RequestMapping(value = exportDetailed, method = { RequestMethod.GET,RequestMethod.POST }, produces = "text/plain;charset=utf-8")
 	public void exportDetailed(String key, HttpServletResponse res) {
 		String fileName = "";
 		OutStock outStock=outStockService.queryModelById(key);
 		Billing billing =billingService.queryModel(outStock.getBillingId());
 		Customer customer=customerService.queryModel(billing.getCustomerId());
 		billing.setOutStock(outStock);
 		billing.setCustomer(customer);
 		
 		Date date = DateUtils.getCurrent();
 		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 		String dateString = dateFormat.format(date);
 		
 		String moduleNo =billing.getBillingNo();
 		// ---------------------------------------------------
 		fileName = moduleNo +"(" + dateString + ").xls";

 		// 声明一个工作薄
 		HSSFWorkbook workbook = new HSSFWorkbook();
 		// 生成一个表格
 		HSSFSheet sheet = workbook.createSheet("订单");

 		// 大标题的样式
 		HSSFCellStyle styleTitle = workbook.createCellStyle(); // 宋体 24
 		styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中格式
 		styleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
 		// 生成一个字体
 		HSSFFont fontTitle = workbook.createFont();
 		fontTitle.setFontName("宋体");
 		fontTitle.setFontHeightInPoints((short) 20);// 设置字体大小
 		fontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
 		styleTitle.setFont(fontTitle);

 		int rowindex = 0;

 		// 大标题
 		HSSFRow rowTitle = sheet.createRow(rowindex);

 		rowTitle.createCell(0).setCellValue("发  货  订  单  信  息");
 	
 		

 		rowTitle.getCell(0).setCellStyle(styleTitle);
 		rowTitle.setHeight((short) 700);// 行高46.5*20

 		// 标题的格式
 		HSSFCellStyle styleSubTitle = workbook.createCellStyle();
 		styleSubTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中格式
 		styleSubTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
 		styleSubTitle.setWrapText(true);// 设置自动换行
 		styleSubTitle.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 设置边框
 		styleSubTitle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
 		styleSubTitle.setBorderRight(HSSFCellStyle.BORDER_THIN);
 		styleSubTitle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
 		HSSFFont fontSubTitle = workbook.createFont();// 宋体 9
 		fontSubTitle.setFontName("宋体");
 		fontSubTitle.setFontHeightInPoints((short) 10);// 设置字体大小
 		fontSubTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
 		styleSubTitle.setFont(fontSubTitle);

 		HSSFRow rowSerialNoTitle = sheet.createRow(++rowindex);
 		rowSerialNoTitle.createCell(0).setCellValue("订单号");
 		rowSerialNoTitle.createCell(1).setCellValue(billing.getBillingNo());
 		rowSerialNoTitle.createCell(3).setCellValue("下单日期");
 		rowSerialNoTitle.createCell(4).setCellValue(dateFormat.format(billing.getCreateDate()));
 		rowSerialNoTitle.createCell(6).setCellValue("开单人");
 		rowSerialNoTitle.createCell(7).setCellValue(billing.getCreateUserName());
 		

 		rowSerialNoTitle.setHeight((short) 500);// 行高40*20

 		for (int i = 0; i < 9; i++) {// 创建空格并赋予样式
 			HSSFCell cell = rowSerialNoTitle.getCell(i);
 			if (cell == null) {
 				rowSerialNoTitle.createCell(i).setCellValue("");
 			}
 			rowSerialNoTitle.getCell(i).setCellStyle(styleSubTitle);
 		}


 		rowSerialNoTitle = sheet.createRow(++rowindex);
 		
 		rowSerialNoTitle.createCell(0).setCellValue("客户");
 		rowSerialNoTitle.createCell(1).setCellValue(billing.getCustomer().getCustomerName());
 		rowSerialNoTitle.createCell(3).setCellValue("客户电话");
 		rowSerialNoTitle.createCell(4).setCellValue(billing.getCustomer().getCustomerTel());
 		rowSerialNoTitle.setHeight((short) 500);// 行高25.5*20
 		
 		for (int i = 0; i < 9; i++) {// 创建空格并赋予样式
 			HSSFCell cell = rowSerialNoTitle.getCell(i);
 			if (cell == null) {
 				rowSerialNoTitle.createCell(i).setCellValue("");
 			}
 			rowSerialNoTitle.getCell(i).setCellStyle(styleSubTitle);
 		}

 		rowSerialNoTitle = sheet.createRow(++rowindex);
 		rowSerialNoTitle.createCell(0).setCellValue("收货地址");
 		rowSerialNoTitle.createCell(1).setCellValue(billing.getAddress());
 		rowSerialNoTitle.setHeight((short) 500);// 行高25.5*20

 		for (int i = 0; i < 9; i++) {// 创建空格并赋予样式
 			HSSFCell cell = rowSerialNoTitle.getCell(i);
 			if (cell == null) {
 				rowSerialNoTitle.createCell(i).setCellValue("");
 			}
 			rowSerialNoTitle.getCell(i).setCellStyle(styleSubTitle);
 		}

 		// 空行
 		rowSerialNoTitle = sheet.createRow(++rowindex);

 		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8));
 		sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
 		sheet.addMergedRegion(new CellRangeAddress(1, 1, 4, 5));
 		sheet.addMergedRegion(new CellRangeAddress(1, 1, 7, 8));
 		sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 2));
 		sheet.addMergedRegion(new CellRangeAddress(2, 2, 4, 8));
 		sheet.addMergedRegion(new CellRangeAddress(3, 3, 1, 8));

 		// 标题2的格式
 		HSSFCellStyle styleSubTitle3 = workbook.createCellStyle();
 		styleSubTitle3.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中格式
 		styleSubTitle3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
 		styleSubTitle3.setWrapText(true);// 设置自动换行
 		styleSubTitle3.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 设置边框
 		styleSubTitle3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
 		styleSubTitle3.setBorderRight(HSSFCellStyle.BORDER_THIN);
 		styleSubTitle3.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
 		HSSFFont fontSubTitle3 = workbook.createFont();// 宋体 10.5 没有10.5设为11
 		fontSubTitle3.setFontName("宋体");
 		fontSubTitle3.setFontHeightInPoints((short) 10);// 设置字体大小
 		fontSubTitle3.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
 		styleSubTitle3.setFont(fontSubTitle3);

 		rowSerialNoTitle = sheet.createRow(++rowindex);
 		rowSerialNoTitle.createCell(0).setCellValue("序号");
 		rowSerialNoTitle.createCell(1).setCellValue("商品编号");
 		rowSerialNoTitle.createCell(3).setCellValue("商品名称");
 		rowSerialNoTitle.createCell(4).setCellValue("规格1");
 		rowSerialNoTitle.createCell(5).setCellValue("规格2");
 		rowSerialNoTitle.createCell(6).setCellValue("数量");
 		rowSerialNoTitle.createCell(7).setCellValue("单价");
 		rowSerialNoTitle.createCell(8).setCellValue("小计");
 		rowSerialNoTitle.setHeight((short) 500);// 行高25.5*20

 		for (int i = 0; i < 9; i++) {// 创建空格并赋予样式
 			HSSFCell cell = rowSerialNoTitle.getCell(i);
 			if (cell == null) {
 				rowSerialNoTitle.createCell(i).setCellValue("");
 			}
 			rowSerialNoTitle.getCell(i).setCellStyle(styleSubTitle3);
 		}

 		// 数据2的格式
 		HSSFCellStyle styleContent3 = workbook.createCellStyle();
 		styleContent3.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中格式
 		styleContent3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直居中
 		styleContent3.setBorderBottom(HSSFCellStyle.BORDER_THIN);// 设置边框
 		styleContent3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
 		styleContent3.setBorderRight(HSSFCellStyle.BORDER_THIN);
 		styleContent3.setWrapText(true);// 设置自动换行
 		HSSFFont fontContent3 = workbook.createFont();// 宋体 12 不加粗
 		fontContent3.setFontName("宋体");
 		fontContent3.setFontHeightInPoints((short) 8);// 设置字体大小
 		styleContent3.setFont(fontContent3);
 		int num=0;
 		Double nullAll=0.0;
 		double realPriceAll=0;
 		HSSFRow row1;
 		for(OutStockShow outStockShow:billing.getOutStock().getOutStockShows()) {
 			for(OutStockCollocation outStockCollocation:outStockShow.getOutStockCollocations()) {
 				row1 = sheet.createRow(++rowindex);
 				row1.setHeight((short) 500);// 行高25.5*20
 				num++;
 				nullAll=nullAll+outStockCollocation.getCollocationNum();
 				realPriceAll=realPriceAll+outStockCollocation.getCollocationNum()*outStockCollocation.getRealPrice();
 				row1.createCell(0).setCellValue(num);
 				row1.createCell(1).setCellValue(outStockShow.getCargo().getCargoNo());
 				row1.createCell(3).setCellValue(outStockShow.getCargo().getCargoName());
 				row1.createCell(4).setCellValue(outStockCollocation.getCollocation().getPropertyNameOne());
 				row1.createCell(5).setCellValue(outStockCollocation.getCollocation().getPropertyNameTwo());
 				row1.createCell(6).setCellValue(outStockCollocation.getCollocationNum());
 				row1.createCell(7).setCellValue(outStockCollocation.getRealPrice());
 				row1.createCell(8).setCellValue(outStockCollocation.getCollocationNum()*outStockCollocation.getRealPrice());
 				

 				for (int j = 0; j < 9; j++) {// 创建空格并赋予样式
 					HSSFCell cell = row1.getCell(j);
 					if (cell == null) {
 						row1.createCell(j).setCellValue("");
 					}
 					row1.getCell(j).setCellStyle(styleContent3);
 				}
 				
 				sheet.addMergedRegion(new CellRangeAddress(rowindex, rowindex, 1, 2));
 			}
 			
 		}
 		sheet.addMergedRegion(new CellRangeAddress(5, 5, 1, 2));
 		
 		// 合计
 		HSSFRow rowCommnets = sheet.createRow(++rowindex);
 		rowCommnets.createCell(0).setCellValue("合计");
 		rowCommnets.createCell(6).setCellValue(nullAll);
 		rowCommnets.createCell(7).setCellValue("");
 		rowCommnets.createCell(8).setCellValue(realPriceAll);
 		rowCommnets.setHeight((short) 500);// 行高39.75*20

 		for (int i = 0; i < 9; i++) {// 创建空格并赋予样式
 			HSSFCell cell = rowCommnets.getCell(i);
 			if (cell == null) {
 				rowCommnets.createCell(i).setCellValue("");
 			}
 			rowCommnets.getCell(i).setCellStyle(styleContent3);
 		}



 		sheet.addMergedRegion(new CellRangeAddress(rowindex, rowindex, 0, 5));


 		// int curColWidth=0;

 		int[] defaultColWidth = { 2000, 3100, 3500, 2500, 2000, 2000, 1300, 1300, 1300, 3500, 1250, 1250, 1250, 1250,
 				1250, 1250, 1250, 1250, 1250 };
 		/* 实际宽度 < 默认宽度的时候、设置为默认宽度 */
 		for (int i = 0; i < 8; i++) {
 			sheet.setColumnWidth(i, defaultColWidth[i]);
 		}

 		OutputStream out = null;
 		try {
 			res.reset();
 			res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
 			res.setContentType("application/vnd.ms-excel; charset=utf-8");
 			// 创建输出位置
 			out = res.getOutputStream();
 			// 输出
 			workbook.write(out);
 			// 清空缓冲区
 			out.flush();
 			// 关闭流，关闭前会清空缓冲区
 			out.close();
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
 		
 	}
}
